package com.ithinks.flow.common.utils.protocol.socket.bio;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @Description
 * @ClassName BioServerHandler
 * @Author MR.LI
 * @date 2020.12.08 16:56
 */
public class BioServerHandler implements Runnable{

    private Socket socket;

    public BioServerHandler(Socket socket){
        this.socket = socket;
    }
    @Override
    public void run() {
        try {
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(),true);

            String message;
            String result;
            while((message = in.readLine()) != null){
                System.out.println("server accept message: "+message);
                result = response(message);
                out.println(result);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //返回给客户端的应答
    public static String response(String msg){
        return "Hello,"+msg+",Now is "+new java.util.Date(
                System.currentTimeMillis()).toString() ;
    }
}
